
goog.provide('esn.dialog.BaseDialog');

goog.require('esn.soy.dialog.baseDialog');
goog.require('goog.array');
goog.require('goog.dom.classes');
goog.require('goog.object');
goog.require('goog.style');
goog.require('goog.ui.CustomButton');
goog.require('goog.ui.Dialog');


/*
 * @param {string=} opt_cssClass CSS class name for the dialog element, also used
 *     as a class name prefix for related elements; defaults to modal-dialog.
 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper; see {@link
 *     goog.ui.Component} for semantics.
 * @constructor
 * @extends {goog.ui.Dialog}
 */
esn.dialog.BaseDialog = function(opt_cssClass, opt_domHelper) {
  goog.base(this);

  this.cssClass_ = opt_cssClass || null;
};
goog.inherits(esn.dialog.BaseDialog, goog.ui.Dialog);

esn.dialog.BaseDialog.EventType = {
  ACCEPT: goog.events.getUniqueId('accept'),
  CANCEL: goog.events.getUniqueId('cancel'),
  CLOSE: goog.events.getUniqueId('close'),
  START_NETWORK: goog.events.getUniqueId('start_network'),
  NO: goog.events.getUniqueId('no'),
  OK: goog.events.getUniqueId('ok'),
  YES: goog.events.getUniqueId('yes')
};

esn.dialog.BaseDialog.Button = {
  ACCEPT: 'Accept',
  CANCEL: 'Cancel',
  CLOSE: 'Close',
  NO: 'No',
  OK: 'Ok',
  START_NETWORK: 'Start building my network!',
  YES: 'Yes'
};

esn.dialog.BaseDialog.EventTypeMap_ = goog.object.create(
    esn.dialog.BaseDialog.Button.ACCEPT, esn.dialog.BaseDialog.EventType.ACCEPT,
    esn.dialog.BaseDialog.Button.CANCEL, esn.dialog.BaseDialog.EventType.CANCEL,
    esn.dialog.BaseDialog.Button.CLOSE, esn.dialog.BaseDialog.EventType.CLOSE,
    esn.dialog.BaseDialog.Button.NO, esn.dialog.BaseDialog.EventType.NO,
    esn.dialog.BaseDialog.Button.OK, esn.dialog.BaseDialog.EventType.OK,
    esn.dialog.BaseDialog.Button.START_NETWORK,
        esn.dialog.BaseDialog.EventType.START_NETWORK,
    esn.dialog.BaseDialog.Button.YES, esn.dialog.BaseDialog.EventType.YES);

esn.dialog.BaseDialog.prototype.customcustombuttons_;

esn.dialog.BaseDialog.prototype.contentContainerEl_;

esn.dialog.BaseDialog.prototype.buttonContainerEl_;

esn.dialog.BaseDialog.prototype.enterDocument = function() {
  goog.base(this, 'enterDocument');

  this.getHandler().listen(
      this,
      goog.ui.Component.EventType.ACTION,
      this.handleButtonClick_);
};

/**
 * Allows arbitrary HTML to be set in the content element.
 * @param {string} html Content HTML.
 */
esn.dialog.BaseDialog.prototype.setContent = function(content) {
  // Add button container to our dialog html.
  var html = esn.soy.dialog.baseDialog.content({Content: content});
  goog.base(this, 'setContent', html);

  this.contentContainerEl_ =
      goog.dom.getElementByClass('contentContainer', this.getContentElement());
  this.buttonContainerEl_ =
      goog.dom.getElementByClass('buttonContainer', this.getContentElement());

  // Hide default buttons.
  var modalButtons =
      goog.dom.getElementByClass('modal-dialog-buttons', this.getElement());
  goog.style.showElement(modalButtons, false);  

  if (this.custombuttons_) {
    this.renderButtons_();
  }

  // Add custom CSS class.
  if (this.cssClass_) {
    goog.dom.classes.add(this.getElement(), this.cssClass_);
  }
};

/**
 * @param {Array.<esn.dialog.BaseDialog.Button>}
 */
esn.dialog.BaseDialog.prototype.setButtons = function(buttonTypes) {
  this.custombuttons_ = goog.array.map(buttonTypes, function(buttonType) {
    var button = new goog.ui.CustomButton(buttonType);
    this.addChild(button);
    button.setModel(buttonType);
    return button;
  }, this);

  if (this.buttonContainerEl_) {
    this.renderButtons_();
  }
};

esn.dialog.BaseDialog.prototype.renderButtons_ = function() {
  goog.array.forEach(this.custombuttons_, function(button, index) {
    button.render(this.buttonContainerEl_);
    // Make the first button our default button.
    if (index == 0) {
      goog.dom.classes.add(button.getElement(), 'defaultButton');
    }
  }, this);
};

esn.dialog.BaseDialog.prototype.handleButtonClick_ = function(e) {
  var buttonType = e.target.getModel();
  var eventType = esn.dialog.BaseDialog.EventTypeMap_[buttonType];
  this.dispatchEvent(eventType);
};
